﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;

namespace Spinning.Spinning.Scenes.SinglePlayer
{
    class SinglePlayerAnimationBoss1End : BaseScene
    {
        AnimationSprite Sprite, TextSprite;
        Sprite blackSprite;
        TextEntity te;
        public override void OnLoad()
        {
            base.OnLoad();

            Sprite = new AnimationSprite();
            Sprite.textureName = "animation/stage1Boss/story08"; //!
            Sprite.OnLoad();
            Sprite.ZIndex = Constants.background;
            Sprite.SetAnimation(new Point(1, 1));
            Sprite.Origin = new Vector2(0.5f);
            Sprite.Position = new Vector2(240, 400);
            this.Attach(Sprite);

            TextSprite = new AnimationSprite();
            TextSprite.textureName = "animation/stage1Boss/story08_txt"; //!
            TextSprite.OnLoad();
            TextSprite.ZIndex = Constants.background + 1;
            TextSprite.SetAnimation(new Point(1, 1));
            TextSprite.Origin = new Vector2(0.5f);
            TextSprite.Position = new Vector2(240, 400);
            TextSprite.Alpha = 0;
            this.Attach(TextSprite);

            blackSprite = new Sprite();
            blackSprite.textureName = "animation/black";
            blackSprite.OnLoad();
            blackSprite.ZIndex = Constants.uibackground;
            blackSprite.Origin = new Vector2(0.5f);
            blackSprite.Position = new Vector2(240, 400);
            blackSprite.Scale = new Vector2(480, 800);
            this.Attach(blackSprite);

            te = new TextEntity();
            ResourceManager.ContentRoot = "";
            te.fontString = "Century Gothic";
            te.OnLoad();
            te.Origin = new Vector2(0.5f);
            te.Position = new Vector2(200, 20);
            te.Color = Color.Blue;
            te.Visable = false;
            te.ZIndex = Constants.debugText;
            Attach(te);
        }

        int tempTimer;
        int animationCounter = 0; //單位:秒/10
        int changetime = 15;  //換圖後文字出來的時機
        int changetime1 = 35; //文字出來後
        int textAlphaTime = 5;
        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            int delta = gameTime.ElapsedGameTime.Milliseconds;
            tempTimer += delta;
            if (tempTimer / 100 >= 1)
            {
                tempTimer = tempTimer % 100;
                animationCounter += 1;
            }

            textSpriteFadeIn(changetime * 1);

            blackSpriteFade(1);
            blackSpriteFade(changetime * 1 + changetime1 * 1);

            if (animationCounter == changetime * 1)
            {
                TextSprite.Alpha = 1;
            }

            if (animationCounter == changetime * 1 + changetime1 * 1)
            {
                Game1.stageManager.GoToScene("reward");
            }

            //te.Value = "" + animationCounter;
        }

        private void textSpriteFadeIn(int timing)
        {
            if (animationCounter <= timing && animationCounter > timing - textAlphaTime)//10
            {
                TextSprite.Alpha = ((float)(animationCounter - (timing - textAlphaTime)) / textAlphaTime);
            }
        }

        private void blackSpriteFade(int timing)
        {
            if (animationCounter <= timing && animationCounter > timing - textAlphaTime)//10
            {
                blackSprite.Alpha = ((float)(animationCounter - (timing - textAlphaTime)) / textAlphaTime);
            }
            if (animationCounter > timing && animationCounter <= timing + textAlphaTime)//10
            {
                blackSprite.Alpha = ((float)((timing + textAlphaTime) - animationCounter) / textAlphaTime);
            }
        }

        public override BaseScene Clone()
        {
            BaseScene scene = new SinglePlayerAnimationBoss1End();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            return scene;
        }
    }
}
